import http from '../../apis/api.js';
let self;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    machineList:[],/**机器列表 */
    levelList:[1,2,3],/**故障 */
    imageList: [],/**图片列表 */
    formData:{
      macId:'',
      machineName: '', 
      problemLevel:'',
      problemType:'1',
      problem:'',
    },
    error:{
      machineName: '',
      problem:'',
      problemLevel:''
    },
    submitDisable:false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    self=this;
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  checked(name) {
    /**表单输入验证 */
    switch (name) {
      case 'machineName':
        if (self.data.formData.machineName === '') {
          self.data.error.machineName = "请选择机器编号"
          return false
        }
        self.data.error.machineName = '';
        return true;
        break;
      case 'problemLevel':
        if (self.data.formData.problemLevel === '') {
          self.data.error.problemLevel = "请选择故障等级"
          return false
        }
        self.data.error.problemLevel = '';
        return true;
        break;
      case 'problem':
        if (self.data.formData.problem === '') {
          self.data.error.problem = "故障现象不能为空"
          return false
        }
        self.data.error.problem = '';
        return true;
        break;
    }
  },
  /**获取input的值*/ 
  getValus(e) {
    let type = e.currentTarget.dataset.type;
    switch (type) {
      case 'problem':
        self.data.formData.problem = e.detail.value;
        break;
    }
    self.setData({
      formData: self.data.formData
    })
  },
  submitInfo(){
    self.setData({
      submitDisable:true
    })
    if (self.checked('machineName') && self.checked('problemLevel') && self.checked('problem')) {
      let postData = {
        macId: self.data.formData.macId,
        problem: self.data.formData.problem,
        problemType: self.data.formData.problemType,
        problemLevel: self.data.formData.problemLevel
      };
      let imageList = this.selectComponent('#imageId').data.imageList; // 页面获取自定义组件实例
      if (imageList.length > 0) {
        imageList.forEach((item, index) => {
          postData['picture' + (index+1)] = item.path;
        })
      }
      http.addRepair(postData).then(res=>{
        wx.showToast({
          title: '报修成功',
          icon: 'success',
          duration: 2000,
          success(){
            wx.redirectTo({
              url:'/pages/repairList/repairList'
            })
          },
          complete(){
            self.setData({
              submitDisable: false
            })
          }
        })
      },error=>{
        self.setData({
          submitDisable: false
        })
      })
    }else{
      self.setData({
        error: self.data.error,
        submitDisable:false
      })
    }
  },
  scanMachineCode(){
    wx.scanCode({
      success(res) {
        self.getMachineByMacCode({
          macCode: res.result
        })
      }
    })
  },
  /**根据机器macCode查询机器信息 */
  getMachineByMacCode(data) {
    http.getMachineByMacCode(data).then(res => {
      self.data.formData.machineName = `${res.content.plasmaStationName}-${res.content.type}-${res.content.macCode}`;
      self.data.formData.macId = res.content.id;
      self.setData({
        formData: self.data.formData
      })
    })
  },
  /**点击选择故障类型 */
  selectProblemType(e){
    self.data.formData.problemType = e.currentTarget.dataset.itemvalue;
    self.setData({
      formData: self.data.formData
    })
  },
  /**选择故障等级 */
  bindLevelChange(e){
    self.data.formData.problemLevel = self.data.levelList[e.detail.value];
    self.setData({
      formData: self.data.formData
    })
  }
})